﻿using D.B.Data.API.Applications.Command.RoleMenusCommand;
using D.B.Data.Domain;
using D.B.Data.Domain.DTO.Output;
using D.B.Data.ErrorCode;
using D.B.Data.Infrastructuse;
using MediatR;
using Microsoft.EntityFrameworkCore;

namespace D.B.Data.API.Applications.CommandHandler.RoleMenusCommandHandler
{
    public class GetRoleCommandHandler : IRequestHandler<GetRoleCommand, APIResult<List<RoleTreeDTO>>>
    {
        private readonly IBaseRepository<RoleModel> _repository;

        public GetRoleCommandHandler(IBaseRepository<RoleModel> repository)
        {
            _repository = repository;
        }

        public Task<APIResult<List<RoleTreeDTO>>> Handle(GetRoleCommand request, CancellationToken cancellationToken)
        {
            var res = _repository.GetAll().AsNoTracking().Where(s => !s.IsDel).Select(item=>new RoleTreeDTO
            {
                  id = item.Id,
                  label=item.RoleName
            });
            APIResult<List<RoleTreeDTO>> aPIResult = new APIResult<List<RoleTreeDTO>>()
            {
                Code = EnumCode.Ok,
                Data = res.ToList(),
                Msg = "成功"
            };

            return Task.FromResult(aPIResult);



        }
    }
}
